-module(pb_system).

-export([pack/2, pack_msg/2, unpack/2, unpack_msg/2]).


%% 服务器时间
pack(1010, {Time}) ->
	Bin1 = pb:encode(u32, Time),
	BinData = <<Bin1/binary>>,
	{ok, pb:msg(1010, BinData)};

%% 服务器断开连接
pack(1020, {Code}) ->
	Bin1 = pb:encode(u16, Code),
	BinData = <<Bin1/binary>>,
	{ok, pb:msg(1020, BinData)};

%% 错误代码
pack(1030, {Code}) ->
	Bin1 = pb:encode(u16, Code),
	BinData = <<Bin1/binary>>,
	{ok, pb:msg(1030, BinData)};

pack(_Cmd, _Data) -> 
	{error, {unknown_command, _Cmd, _Data}}.


%% 服务器时间
pack_msg(1010, {Time}) ->
	Bin1 = pb:encode(u32, Time),
	<<Bin1/binary>>;

%% 服务器断开连接
pack_msg(1020, {Code}) ->
	Bin1 = pb:encode(u16, Code),
	<<Bin1/binary>>;

%% 错误代码
pack_msg(1030, {Code}) ->
	Bin1 = pb:encode(u16, Code),
	<<Bin1/binary>>;

pack_msg(_Cmd, _Data) -> 
	{error, {unknown_command, _Cmd, _Data}}.


%% 服务器时间
unpack(1010, _Bin0) ->
	{Time, _Bin1} = pb:decode(u32, _Bin0),
	{ok, {Time}};

%% 服务器断开连接
unpack(1020, _Bin0) ->
	{Code, _Bin1} = pb:decode(u16, _Bin0),
	{ok, {Code}};

%% 错误代码
unpack(1030, _Bin0) ->
	{Code, _Bin1} = pb:decode(u16, _Bin0),
	{ok, {Code}};

unpack(_Cmd, _Bin) -> 
	{error, {unknown_command, _Cmd, _Bin}}.

%% 服务器时间
unpack_msg(1010, _Bin0) ->
	{Time, _Bin1} = pb:decode(u32, _Bin0),
	{{Time},_Bin1};

%% 服务器断开连接
unpack_msg(1020, _Bin0) ->
	{Code, _Bin1} = pb:decode(u16, _Bin0),
	{{Code},_Bin1};

%% 错误代码
unpack_msg(1030, _Bin0) ->
	{Code, _Bin1} = pb:decode(u16, _Bin0),
	{{Code},_Bin1};

unpack_msg(_Cmd, _Bin) -> 
	{error, {unknown_command, _Cmd, _Bin}}.
